package com.zjt.service.user;

import com.zjt.dao.user.IClientUserDao;
import com.zjt.entity.ClientUser;
import com.zjt.utils.DateUtil;
import com.zjt.web.api.vo.base.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

/**
 * 物码中心数据采集的DAO interface;
 * 
 * @author xueyanbeijing
 */
@Component
public class ClientUserService implements IClientUserService {
    private static Logger logger = LoggerFactory.getLogger(ClientUserService.class);
    private IClientUserDao clientUserDao;
    @Autowired
    public void setClientUserDao(IClientUserDao clientUserDao) {
        this.clientUserDao = clientUserDao;
    }

    @Override
    public ClientUser registerClientUser(String nickName, String email, String passwd, String mdn, String sex,
                                   String version, String platform, String imei) {
        ClientUser clientUser = new ClientUser();
        try
        {
            clientUser.setNickName(nickName);
            clientUser.setEmail(email);
            clientUser.setPasswd(passwd);
            clientUser.setMdn(mdn);
            clientUser.setSex(sex);
            clientUser.setVersion(version);
            clientUser.setPlatform(platform);
            clientUser.setImei(imei);
            clientUser.setCreateDate(DateUtil.getCurrentDate());
            clientUser.setUpdateDate(DateUtil.getCurrentDate());
            clientUser.setEnableFlag("Y");
            clientUser.setLoginCount(1);
            clientUser.setStatus(0);
            clientUser.setLastLoginTime(DateUtil.getCurrentDate());
            clientUser = this.clientUserDao.saveAndFlush(clientUser);
        }catch (Exception e){
            logger.error("注册异常;service:="+clientUser.toString());
        }
        return clientUser;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser loginClientUserByUserName(String userName, String passwd) {
        ClientUser clientUser = null;
        ClientUser emailClientUser = null;
        ClientUser mdnClientUser = null;
        long tempUserId = -1;
        boolean doFlag = false;
        try
        {
            if (userName.matches("[0-9]*")==false)  //不是数字
            {
                emailClientUser = this.clientUserDao.findByEmailAndPasswd(userName.trim(),passwd);
                if (emailClientUser!=null)
                {
                    emailClientUser.setLoginCount(emailClientUser.getLoginCount()+1);
                    emailClientUser.setLastLoginTime(DateUtil.getCurrentDate());
                    this.clientUserDao.saveAndFlush(emailClientUser);
                    doFlag = true;
                    tempUserId = emailClientUser.getId();
                }
            }else //是数字 只能用ID和手机号码验证
                clientUser = this.clientUserDao.findByIdAndPasswd(Long.parseLong(userName.trim()),passwd);
            if (clientUser==null)
            {
                mdnClientUser = this.clientUserDao.findByMdnAndPasswd(userName.trim(),passwd);
                if (mdnClientUser!=null)
                {
                    emailClientUser.setLoginCount(mdnClientUser.getLoginCount()+1);
                    emailClientUser.setLastLoginTime(DateUtil.getCurrentDate());
                    this.clientUserDao.save(emailClientUser);
                    doFlag = true;
                    tempUserId = mdnClientUser.getId();
                }
            }
            else
            {
                clientUser.setLoginCount(clientUser.getLoginCount()+1);
                clientUser.setLastLoginTime(DateUtil.getCurrentDate());
                this.clientUserDao.saveAndFlush(clientUser);
                doFlag = true;
                tempUserId = clientUser.getId();
            }

        }catch (Exception e){
            doFlag = false;
        }
        if (doFlag==true) return this.clientUserDao.findOne(tempUserId);
        else return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser getClientUserById(Long userId) {
        return this.clientUserDao.findOne(userId);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean checkExistByUserId(long userId) {
        return this.clientUserDao.exists(userId);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser updateClientUser(long userId, String nickName, String email, String passwd, String mdn, String sex,
                                       String version, String platform, String imei) {
        ClientUser clientUser = this.clientUserDao.findOne(userId);
        clientUser.setLastLoginTime(DateUtil.getCurrentDate());
        try
        {
            if (clientUser!=null)
            {
                if (version!=null)
                    clientUser.setVersion(version);
                if (platform!=null)
                    clientUser.setPlatform(platform);
                if (imei!=null)
                    clientUser.setImei(imei);
                if (passwd!=null)
                    clientUser.setPasswd(passwd);
                if (mdn!=null)
                    clientUser.setMdn(mdn);
                if (nickName!=null)
                    clientUser.setNickName(nickName);
                if (email!=null)
                    clientUser.setEmail(email);
                if (sex!=null)
                    clientUser.setSex(sex);
            }
            clientUser = this.clientUserDao.saveAndFlush(clientUser);
        }catch (Exception e){
        }
        return clientUser;
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser getClientUserByImei(String imei) {
        return this.clientUserDao.findByImei(imei);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser getClientUserByEmail(String email) {
        return this.clientUserDao.findByEmail(email);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ClientUser getClientUserByUserName(String userName) {
        ClientUser clientUser = null;
        ClientUser emailClientUser = null;
        ClientUser mdnClientUser = null;
        long tempUserId = -1;
        boolean doFlag = false;
        try
        {
            if (userName.matches("[0-9]*")==false)  //不是数字
            {
                emailClientUser = this.clientUserDao.findByEmail(userName.trim());
                if (emailClientUser!=null)
                {
                    doFlag = true;
                    tempUserId = emailClientUser.getId();
                }
            }else //是数字 只能用ID和手机号码验证
                clientUser = this.clientUserDao.findOne(Long.parseLong(userName.trim()));
            if (clientUser==null)
            {
                mdnClientUser = this.clientUserDao.findByMdn(userName.trim());
                if (mdnClientUser!=null)
                {
                    doFlag = true;
                    tempUserId = mdnClientUser.getId();
                }
            }
            else
            {
                doFlag = true;
                tempUserId = clientUser.getId();
            }

        }catch (Exception e){
            doFlag = false;
        }
        if (doFlag==true) return this.clientUserDao.findOne(tempUserId);
        else return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Page<ClientUser> getClientUserList(PageVO pageVO) {
        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize());
        Page<ClientUser> pageList = null;
        pageList = this.clientUserDao.findAll(pageRequest);
        return pageList;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
